<template>
	<view>
		<u-navbar safeAreaInsetTop placeholder fixed autoBack>
			<view class="u-nav-slot color_ling text_weight" slot="center">
				自助餐點餐
			</view>
			<view class="u-nav-slot" slot="right">
				<u-icon name="share" color="#333" size="24"></u-icon>
			</view>
		</u-navbar>
		<u-sticky bgColor="#fff">
			<view class=" mainpadding">
				<view class="flexbetween">
					<view class="flexleft">
						<u-icon name="grid-fill" color="#ffb800" size="28"></u-icon>
						<view class="huang_text titletext margin_left1">桌號:{{cdbInfo.ordergoods.desk}}</view>
					</view>
					<view class="xiaohong flexleft titletext" style="line-height: 32rpx;">
						<text>剩餘時間:</text>
					<u-count-down :time="Number(cdbInfo.shoptable.dc_endtime)*1000-Number(new Date())" format="HH:mm:ss"></u-count-down></view>
				</view>
			</view>
			<view class="mainpadding2">
				<scroll-view class="white_nowrap" scroll-x>
					<view class="flexleft">
						<view class="margin_right2" v-for="(item,index) in shop_class">
							<view :class="item.isChecked?'cateSelect':'cateNoSelect'" @click="selectCate(item)">
								{{item.name}}
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-sticky>
		<view class="mainpadding2">
			<view class="mainpadding_top ffffff radius" v-for="item in list.data" :key="item.id" @click="guigeShow(item)">
			<!-- <view class="mainpadding_top ffffff radius" v-for="item in list.data" :key="item.id" @click="jumpdetail('/pages_mine/zizhucxq?id=',1)"> -->
				<view class="flexleft">
					<image :src="item.logoimage_text" class="margin_right3" mode="" style="width: 180rpx;height: 180rpx;min-width: 180rpx;"></image>
					<view class="" style="width: 100%;">
						<view class="xiaohei main_size">{{item.name}}</view>
						<view class="margin_top2 flexbetween">
							<text class="stateBox text_e color1">限點:{{item.xd_num==-1?'不限':item.xd_num}}</text>
							<text class="xiaohong">已點：{{item.yd_num}}</text>
						</view>
						<view class="flexbetween">
							<view class="xiaohei margin_top2 main_size">${{item.new_price}}</view>
							<view class="addbtn">+1</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="footerBox" >
			<view class="footerPirce flexbetween"  @click="gocart()">
				<view class="flexbetween" style="width: 100%;">
					<view class="flexleft paddingtb padding_left3">
						<view class="cart margin_right3">
							<view class="num" v-text="carlistAll.length"></view>
						</view>
						<view class="line_r margin_right2"></view>
						<view>
							<text class="color_ling text_d">合計：</text>
							<text class="totalPrice">$</text>
							<text class="totalPrice" style="font-size: 36rpx;" v-text="carallprice"></text>
						</view>
					</view>
				</view>
				<view class="settlement">查看</view>
			</view>
		</view>
		<u-popup :show="show" mode="center" @close="show=false" :round="7">
			<view class=""  style="min-width: 700rpx;max-width: 700rpx;box-sizing: border-box;">
				<view class="flexcenter">
					<image style="width:100%;height:650rpx;border-radius: 20rpx 20rpx 0 0;" :src="currentitem.logoimage_text" mode="">
					</image>
				</view>
				<view class="mainpadding">
					<view class="">
						<view class="hei_text titletext ">{{currentitem.name}}</view>
						<view class="huang_text margin_top2 fonweight">${{allprice}}</view>
					</view>
					<!-- 選擇规格 -->
					<view class="">
						<view class="margin_top5" v-for="item in currentitem.goods_spu" :key="item.id">
							<view class="ershil zhonghei fonweight">{{item.name}}</view>
							<view class="flexleft flex_wrap">
								<view class="flexcenter margin_top margin_right2 " :class="item.spunat_id==ite.id?'ggyxz':'ggwxz'" v-for="ite in item.goods_spunat" :key="ite.id" @click="checkitem(item,ite)">{{ite.name}}&nbsp;&nbsp;&nbsp;${{ite.money}}</view>
							</view>
						</view>
						<view class="flexbetween margin_top5">
							<view class="ershil fonweight xiaohei">購買數量</view>
							<u-number-box button-size="36" color="#000000" bgColor="#F7F7F7" iconStyle="color: #fff"
								v-model="buynumber"></u-number-box>
						</view>
					</view>
					<view class="margin_top5 flexbetween">
						<view class="submitbtn" style="width: 45%;background-color: #dee5eb;" @click="show=false">取消</view>
						<view class="submitbtn" style="width: 45%;" @click="addcar()">確定</view>
					</view>
					
				</view>
			</view>
			
		</u-popup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				shop_class:[],
				show:false,
				buynumber:1,
				cdbtc_id:"",
				shop_id:"",
				shopclassify_id:"",
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				currentitem:{},
				carlistAll:[],
				cdbInfo:{}
			}
		},
		onLoad(options) {
			this.cdbtc_id = options.cdbtc_id
			this.shop_id = options.shop_id
			this.getShopclass()
		},
		onShow() {
			this.getcarList()
			this.getcdbInfo()
			this.init()
		},
		watch:{
			"show":{
				deep:true,
				handler(newval){
					if(newval==false){
						this.buynumber = 1
					}
				}
			}
		},
		computed:{
			allprice(){
				if(this.currentitem.new_price){
					let price = this.currentitem.new_price
					this.currentitem.goods_spu.forEach(item=>{
						price = Number(item.spunat_price)+Number(price)
					})
					return price * this.buynumber
				}else{
					return 0
				}
				
			},
			carallprice(){
				let price = 0
				this.carlistAll.forEach(item=>{
					price = Number(item.price)*item.num+Number(price)
				})
				return price 
			}
		},
		methods: {
			gocart(){
				uni.navigateTo({
					url: "/pages_mine/zizhucqrdd?cdbtc_id=" + this.cdbtc_id + "&shop_id=" + this.shop_id
				})
			},
			addcar(){
				let goods_spunat_ids = []
				let goods_spunat_name = []
				this.currentitem.goods_spu.forEach(item=>{
					if(item.spunat_id!=""){
						goods_spunat_ids.push(item.spunat_id)
						goods_spunat_name.push(item.spunat_name)
					}
				})
				let data = {
					shop_id: this.shop_id,
					order_make_id:uni.getStorageSync("order_make_id"),
					goods_id:this.currentitem.id,
					goods_spunat_ids:goods_spunat_ids.join(),
					goods_spunat_name:goods_spunat_name.join(),
					num:this.buynumber,
					price:this.allprice/this.buynumber,
					logoimage:this.currentitem.logoimage
				}
				this.http.request('/api/cdb/addCdbCart', 'GET', data).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						this.show = false
						this.getcarList()
						this.init()
					}
				})
			},
			getcarList(){
				this.http.request('/api/cdb/cdbcartIndex', 'GET', {
					shop_id: this.shop_id,
					order_make_id:uni.getStorageSync("order_make_id"),
				}).then(res => {
					this.carlistAll = res.data
				})
			},
			getcdbInfo(){
				this.http.request('/api/cdb/getShoptable', 'GET', {
					order_make_id:uni.getStorageSync("order_make_id"),
				}).then(res => {
					this.cdbInfo = res.data
				})
			},
			guigeShow(item){
				let djs = Number(this.cdbInfo.shoptable.dc_endtime) * 1000 - Number(new Date())
				if(djs<0){
					httpRequest.toast("很抱歉  點餐時間已到 無法繼續點餐")
					return false
				}
				this.show = true
				if(item.goods_spu.length){
					item.goods_spu.forEach(item=>{
						this.$set(item,"spunat_id","")
						this.$set(item,"spunat_price","")
						this.$set(item,"spunat_name","")
					})
				}
				this.currentitem = item
			},
			checkitem(item,ite){
				if(item.spunat_id==ite.id){
					item.spunat_id=""
					item.spunat_price=""
					item.spunat_name=""
				}else{
					item.spunat_id=ite.id
					item.spunat_price=ite.money
					item.spunat_name=ite.name
				}
			},
			init(isPage, page) {
				let _this = this
				httpRequest.request('/api/cdb/goodsIndex', 'GET', {
					page: page || 1,
					limit: _this.limit,
					cdbtc_id:this.cdbtc_id,
					shopclassify_id:this.shopclassify_id,
					shop_id: this.shop_id,
					order_make_id:uni.getStorageSync("order_make_id"),
				}).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			getShopclass(){
				this.http.request('/api/shop/shopCategory', 'GET', {
					shop_id: this.shop_id
				}).then(res => {
					if (res.data.length) {
						res.data.forEach(item => {
							this.$set(item, "isChecked", false)
						})
						res.data.unshift({
							id: "",
							name: "全部",
							isChecked: true
						})
					} else {
						res.data = [{
							id: "",
							name: "全部",
							isChecked: true
						}]
					}
					this.shop_class = res.data
				})
			},
			selectCate(value) { //商品選擇分类
				this.shop_class.forEach(item => {
					item.isChecked = false
				})
				value.isChecked = true
				this.shopclassify_id = value.id
				this.init()
			},
			

		}
	}
</script>

<style lang="scss" scoped>
	::v-deep.u-count-down__text{
		font-size: 32rpx !important;
		line-height: 32rpx !important;
		color: #ff0000 !important;
	}
	// 分類篩選
	.cateNoSelect {
		padding: 5rpx 20rpx;
		line-height: 53rpx;
		line-height: 53rpx;
		text-align: center;
		background: #EFEFEF;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 1rpx solid #EFEFEF;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}
	
	.cateSelect {
		padding: 5rpx 20rpx;
		line-height: 53rpx;
		text-align: center;
		background: rgba(255, 92, 0, 0.1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 1rpx solid #FF5C00;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FF5C00;
	}
	.ggwxz {
		padding: 9rpx 30rpx;
		background: #F5F5F5;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #F5F5F5;
		font-weight: 500;
		font-size: 26rpx;
		color: #666666;
	}
	
	.ggyxz {
		padding: 9rpx 30rpx;
		background: rgba(255, 147, 65, 0.1);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #FF9341;
		font-weight: 500;
		font-size: 26rpx;
		color: #FF9341;
	}
.stateBox {
		padding: 10rpx 30rpx;
		text-align: center;
		border-radius: 15rpx 15rpx 15rpx 15rpx;
		font-size: 24rpx;
	}
.color1 {
		color: #FF8505;
		background: rgba(255, 133, 5, 0.1);
	}
	.addbtn{
		background-color: #dee5eb;
		border-radius: 10rpx;
		padding: 10rpx 50rpx;
	}
	.gwcbox{
		background-color: #7eb1ea;
		color: #fff;
		padding: 15rpx 0;
		border-radius: 20rpx;
	}
	// 底部
	.footerBox {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		bottom: 40rpx;
	
		.footerPirce {
			width: 708rpx;
			height: 101rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 53rpx 53rpx 53rpx 53rpx;
		}
	
		.cart {
			width: 40.5rpx;
			height: 42.58rpx;
			background-image: url("@/static/images/index/cart_num.png");
			background-size: 100% 100%;
			position: relative;
		}
	
		.num {
			width: 35rpx;
			height: 35rpx;
			line-height: 35rpx;
			text-align: center;
			background: #FF5C00;
			border-radius: 50%;
			position: absolute;
			right: -20rpx;
			top: -20rpx;
			font-size: 17rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	
		.line_r {
			width: 0;
			height: 42rpx;
			border-right: 2rpx dashed rgba(0, 0, 0, 1);
		}
	
		.totalPrice {
			font-size: 26rpx;
			font-family: DIN Next LT Pro-Medium;
			font-weight: 500;
			color: rgba(253, 70, 62, 1);
		}
	
		.settlement {
			width: 178rpx;
			min-width: 178rpx;
			max-width: 178rpx;
			height: 101rpx;
			line-height: 101rpx;
			text-align: center;
			background: #FFB800;
			border-radius: 0rpx 53rpx 53rpx 0rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #000000;
		}
		.settlementleft{
			width: 150rpx;
			height: 101rpx;
			line-height: 101rpx;
			text-align: center;
			background: #ff8505;
			border-radius: 53rpx 0 0rpx 53rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #fff;
		}
	}
</style>